﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TVShows.Data;
using TVShows.DataModels;
using TVShows.Data.ITVShowAdapters.Interfaces;
using TVShows.Data.ITVShowAdapters.Adapters;

namespace TVShows.Web.Controllers
{
    public class TVShowController : Controller
    {
        ITVShowAdapter _adapter;
        public TVShowController()
        {
            _adapter = new TVShowAdapter();
        }
        public TVShowController(ITVShowAdapter adapter)
        {
            _adapter = adapter;
        }
        [HttpGet]
        public ActionResult AddShow()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddShow(string name, string image)
        {
            TVShow show = new TVShow();
            show.Name = name;
            show.Image = image;
            show = _adapter.AddTVShow(show);
            return RedirectToAction("Details/"+ show.Id);
        }
        public ActionResult Details(int id)
        {
            TVShow show = _adapter.GetTVShow(id);
            return View(show);
        }
        [HttpGet]
        public ActionResult EditShow(int id)
        {
            TVShow show = _adapter.GetTVShow(id);
            return View(show);
        }

        [HttpPost]
        public ActionResult EditShow(TVShow show)
        {
            show = _adapter.UpdateTVShow(show);
            return RedirectToAction("Details/" + show.Id);
        }

        public ActionResult DeleteShow(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                TVShow show = db.TVShows.Find(id);
                db.TVShows.Remove(show);
                db.SaveChanges();
            }
            return Redirect("/Home");
        }
	}
}